package com.im.immodel.config

import android.graphics.Color
import android.os.Bundle
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextUtils
import android.text.method.LinkMovementMethod
import android.text.style.ForegroundColorSpan
import android.text.style.ImageSpan
import android.widget.TextView
import com.hotpot.sdk.room.entity.receive.*
import com.hotpot.sdk.utils.*
import com.hotpot.sdk.utils.Constants.Companion.NOT_FRIEND
import com.hotpot.sdk.utils.Constants.Companion.filterSystemMessages
import com.hotpot.uikit.R
import com.hotpot.uikit.talk.config.listener.IMRecyclerProxy
import com.hotpot.uikit.talk.entity.DingMsgEntity
import com.hotpot.uikit.talk.entity.IMSummaryEntity
import com.hotpot.uikit.talk.entity.InnMsgEntity
import com.hotpot.uikit.talk.group.member.NewMemberContent
import com.hotpot.uikit.utils.ImConfig
import com.hotpot.uikit.utils.Utils
import com.hotpot.uikit.utils.html.MyHtml
import com.qgb.zcore.commonconfig.FriendRequestsConfig
import com.qgb.zcore.mmkvutil.CacheMarker
import com.qgb.zcore.mmkvutil.MkvUtils
import com.qgb.zcore.router.ContactsRouterPath
import com.qgb.zcore.router.RouteHelper
import com.qgb.zutils.JsonUtils
import com.qgb.zutils.TimeUtils
import com.qgb.zutils.activitylifecycle.AppHelperUtils
import com.zjkj.qgbui.emoji.EmojiUtil
import com.zjkj.qgbui.widgets.atview.ClickSpanUtils
import com.zjkj.qgbui.widgets.atview.interf.ClickSpanListener
import org.json.JSONException
import org.json.JSONObject
import java.util.*

class IMRecyclerConfigPresenter : IMRecyclerProxy {
    override fun setListMessage(
            textView: TextView?,
            msgType: String?,
            lastMsg: String?,
            lastMsgType: String?,
            lastMsgUserName1: String?,
            lastMsgSenderUserId: String?,
            lastMsgUserId: String?,
            draft: String?,
            groupType: String?,
            isStatus: Boolean,
            isDissolveState: Int,
            touchType: Int,
            otherIsRead: Boolean,
            recipient: String?,
            sendStatus: Int
    ) {
        if (TextUtils.isEmpty(msgType)) {
            textView!!.text = ""
            return
        }
        if (TextUtils.equals(msgType, "EMPTY")) {
            textView!!.text = ""
            return
        }
        val lastMsgUserName = if (TextUtils.isEmpty(lastMsgUserName1)) "" else lastMsgUserName1
        var lastMsgShowName = false
        if (lastMsgSenderUserId != null) {
            lastMsgShowName = TextUtils.equals(lastMsgType, GROUP) && !TextUtils.equals(
                    lastMsgSenderUserId,
                    lastMsgUserId
            )
        }
        //比草稿高一级
        if (TextUtils.equals(lastMsgType, GROUP) && isStatus) {
            textView!!.text =
                    textView!!.context.getString(R.string.hpui_you_were_removed_from_the_group_chat_str)
            return
        }
        if (TextUtils.equals(lastMsgType, GROUP) && isDissolveState == 1) {
            textView!!.text =
                    textView!!.context.getString(R.string.hpui_the_group_chat_has_been_disbanded_str)
            return
        }

        var content =
                if (!TextUtils.isEmpty(ImConfig.MSG_TYPE[msgType])) ImConfig.MSG_TYPE[msgType] else lastMsg
        if (TextUtils.equals(Constants.APPROVE, msgType)) {
            try {
                val jsonObject = JSONObject(lastMsg)
                val contentStr = jsonObject.optString("content")
                content = contentStr.split(",").toTypedArray()[0]
            } catch (e: JSONException) {
                e.printStackTrace()
            }
        }
        if (TextUtils.equals(Constants.SYS_NOTICE, msgType)) {
            try {
                val jsonObject = JSONObject(lastMsg)
                val contentStr = jsonObject.optString("message")
                val messageType = jsonObject.optString("messageType")
                if (TextUtils.equals(messageType, "JSON")) {
                    val imSummaryEntity = JsonUtils.toBean(contentStr, IMSummaryEntity::class.java)
                    val summaryDataList = imSummaryEntity.summaryDataList
                    if (summaryDataList != null && !summaryDataList.isEmpty()) {
                        val contentBuilder = StringBuilder()
                        for (summaryEntity in summaryDataList) {
                            contentBuilder.append(summaryEntity.key)
                            contentBuilder.append(":")
                            contentBuilder.append(summaryEntity.value)
                            contentBuilder.append(";")
                        }
                        content = contentBuilder.toString()
                    }
                } else {
                    content = contentStr.split(",").toTypedArray()[0]
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }

        /*草稿优先级
            1、你已被移出该群聊；该群聊已被解散
            2、@消息（有人@我、所有人）
            3、草稿
            4、其他消息；都是最新消息显示*/
        if (touchType == 0) {//0.常规消息 1.@我 2.@所有人 3.未接通话
            if (!TextUtils.isEmpty(draft)) {
                val caoGao = draft!!.split("【@草稿】").toTypedArray()
                val spannable = MyHtml.fromHtml(caoGao[0], MyHtml.FROM_HTML_MODE_LEGACY)
                val spannableString = SpannableString(spannable)
                spannableString.setSpan(
                        ForegroundColorSpan(Color.parseColor("#FF6600")),
                        0,
                        4,
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                )
                textView!!.text = spannableString
                return
            }
        }

        var isCancel = TextUtils.equals(Constants.CANCEL, msgType)
        var isAdmin = false
        val imAccount =
                MkvUtils.get().getString(MkvUtils.UserData, CacheMarker.ImAccount, "")
        try {
            if (!TextUtils.isEmpty(msgType)) {
                if (TextUtils.equals(msgType, Constants.HIDDEN)) {
                    val innMsgEntity = JsonUtils.toBean(lastMsg, InnMsgEntity::class.java)
                    //过滤第一层 msgtype 类型显示
                    content = if (!TextUtils.isEmpty(ImConfig.MSG_TYPE[innMsgEntity.msgType])) {
                        ImConfig.MSG_TYPE[innMsgEntity.msgType]
                    } else {
                        innMsgEntity.content.text
                    }
                    isCancel = TextUtils.equals(Constants.CANCEL, innMsgEntity.msgType)
                    isAdmin = innMsgEntity.isAdmin
                    if (TextUtils.equals(Constants.DELETE, innMsgEntity.msgType)) {
                        //过滤第二层 msgtype 类型显示
                        val contentMsgType = innMsgEntity.content.msgType

                        if (TextUtils.equals(contentMsgType, Constants.HYPERLINKS)) {
                            val hyperlinksEntity = JsonUtils.toBean(innMsgEntity.content.text, HyperlinksEntity::class.java)
                            content = hyperlinksEntity.website
                        } else if (TextUtils.equals(contentMsgType, Constants.DING_READ)) {
                            val dingReadEntity = JsonUtils.toBean(innMsgEntity.content.text, DingReadEntity::class.java)
                            content = if (TextUtils.equals(dingReadEntity.receiver, lastMsgUserId)) {
                                String.format(dingReadEntity.text!!, dingReadEntity.sendName, "你")
                            } else {
                                String.format(dingReadEntity.text!!, "你", dingReadEntity.receiverName)
                            }
                        } else {
                            content = if (!TextUtils.isEmpty(ImConfig.MSG_TYPE[contentMsgType])) {
                                ImConfig.MSG_TYPE[contentMsgType]
                            } else {
                                innMsgEntity.content.text
                            }
                        }
                    }
                } else if (TextUtils.equals(
                                Constants.NOTICE,
                                msgType
                        ) && !TextUtils.isEmpty(lastMsg)
                ) { //群公告会话列表展示处理
                    val jsonObject = JSONObject(lastMsg)
                    val contentStr = jsonObject.getString("content")
                    val spanned = MyHtml.fromHtml(contentStr, MyHtml.FROM_HTML_MODE_LEGACY);
                    content += spanned
                } else if (TextUtils.equals(Constants.NOTICEREPLY, msgType) && !TextUtils.isEmpty(
                                lastMsg
                        )
                ) {//公告评论消息 处理
                    val jsonObject = JSONObject(lastMsg)
                    val contentStr = jsonObject.getString("commentContent")
                    content = contentStr
                } else if (TextUtils.equals(Constants.VOICE, msgType) || TextUtils.equals(
                                Constants.VIDEOCALL, msgType
                        )
                ) { //语音通话、视频通话会话列表展示处理
                    val jsonObject = JsonUtils.toMap(lastMsg)
                    val state = jsonObject["state"].toString()
                    var time = 0
                    if (jsonObject["time"] == null) {
                        textView!!.text = TimeUtils.formatDuringN(time.toLong())
                    } else {
                        if (jsonObject["time"] is String) {
                            textView!!.text = jsonObject["time"].toString()
                        } else {
                            time = jsonObject["time"] as Int
                        }
                    }
                    val isMe = TextUtils.equals(lastMsgSenderUserId, imAccount)
                    var txt = ""
                    when (state) {
                        "over" -> content =
                                content + "通话时长" + TimeUtils.formatDuringN(time.toLong())
                        "busy" -> {
                            txt = if (touchType == 3) {
                                "<font color='#FF6600'>[未接通话]</font>未接通 点击回拨"
                            } else {
                                content + "未接通 点击回拨"
                            }
                            content = if (isMe) content + "对方正忙 点击重拨" else txt
                        }
                        "refuse" -> content += (if (isMe) "对方已拒绝 点击重拨" else "已拒绝 点击回拨")
                        "noResponse" -> {
                            txt = if (touchType == 3) {
                                "<font color='#FF6600'>[未接通话]</font>未接通 点击回拨"
                            } else {
                                content + "未接通 点击回拨"
                            }
                            content = if (isMe) content + "对方无应答 点击重拨" else txt
                        }
                        "cancel" -> {
                            txt = if (touchType == 3) {
                                "<font color='#FF6600'>[未接通话]</font>对方已取消 点击回拨"
                            } else {
                                content + "对方已取消 点击回拨"
                            }
                            content = if (isMe) content + "已取消 点击重拨" else txt
                        }
                    }
                    textView!!.text = MyHtml.fromHtml(content, MyHtml.FROM_HTML_MODE_LEGACY)
                    return
                } else if (TextUtils.equals(msgType, Constants.PASSIVE_EXIT)) {
                    val entity = JsonUtils.toBean(
                            lastMsg,
                            GroupSystemEntity::class.java
                    )
                    var name1 = ""
                    var name2 = ""
                    val list = findNickNameString(entity, lastMsgUserId)
                    list?.toList()!!.forEach {
                        if (TextUtils.equals(it.userId, entity.handler)) {
                            name1 = it.nickName.toString()
                        } else {
                            name2 = if (TextUtils.isEmpty(name2)) {
                                it.nickName.toString()
                            } else {
                                name2 + "、" + it.nickName
                            }
                        }
                    }
                    content = String.format(entity.content!!, name1, name2)
                } else if (TextUtils.equals(msgType, Constants.INVITE_JOIN) || TextUtils.equals(
                                msgType,
                                Constants.SCANQRCODE_JOIN
                        )
                ) {
                    val entity = JsonUtils.toBean(
                            lastMsg,
                            GroupSystemEntity::class.java
                    )
                    var name1 = ""
                    var name2 = ""
                    val list = findNickNameString(entity, lastMsgUserId)
                    list?.toList()!!.forEach {
                        if (TextUtils.equals(it.userId, entity.handler)) {
                            name1 = if (TextUtils.equals(lastMsgUserId, it.userId)) {
                                "你"
                            } else {
                                it.nickName.toString()
                            }
                        } else {
                            name2 = if (TextUtils.isEmpty(name2)) {
                                if (TextUtils.equals(lastMsgUserId, it.userId)) {
                                    "你"
                                } else {
                                    it.nickName.toString()
                                }
                            } else {
                                if (TextUtils.equals(lastMsgUserId, it.userId)) {
                                    "$name2、你"
                                } else {
                                    name2 + "、" + it.nickName
                                }
                            }
                        }
                    }
                    content = String.format(entity.content!!, name1, name2)
                } else if (TextUtils.equals(msgType, Constants.ACTIVE_EXIT) || TextUtils.equals(
                                msgType,
                                Constants.NORMAL_JOIN
                        ) || TextUtils.equals(msgType, Constants.TRANSFER_OWN) || TextUtils.equals(
                                msgType,
                                Constants.INITIATE_GROUP
                        )
                ) {
                    val entity = JsonUtils.toBean(
                            lastMsg,
                            GroupSystemEntity::class.java
                    )
                    var name1 = ""
                    val list = findNickNameString(entity, lastMsgUserId)
                    list?.toList()!!.forEach {
                        if (TextUtils.equals(it.userId, entity.handler)) {
                            name1 = if (TextUtils.equals(lastMsgUserId, it.userId)) {
                                "你"
                            } else {
                                it.nickName.toString()
                            }
                        }
                    }
                    content = String.format(entity.content!!, name1)
                } else if (TextUtils.equals(msgType, Constants.TOUCH)) {
                    textView!!.text = lastSetTouchMsg(
                            lastMsg,
                            lastMsg,
                            lastMsgUserName,
                            lastMsgSenderUserId,
                            lastMsgType,
                            lastMsgUserId,
                            touchType,
                            false,
                            msgType
                    )
                    //是否可以点击
                    //textView.setMovementMethod(LinkMovementMethod.getInstance());
                    textView!!.highlightColor = Color.parseColor("#00000000") //去掉点击后的背景颜色为透明
                    return
                } else if (TextUtils.equals(msgType, Constants.HYPERLINKS)) {
                    val hyperlinksEntity = JsonUtils.toBean(lastMsg, HyperlinksEntity::class.java)
                    content = hyperlinksEntity.website
                } else if (TextUtils.equals(msgType, Constants.DING_READ)) {
                    val dingReadEntity = JsonUtils.toBean(lastMsg, DingReadEntity::class.java)
                    content = if (TextUtils.equals(dingReadEntity.receiver, lastMsgUserId)) {
                        String.format(dingReadEntity.text!!, dingReadEntity.sendName, "你")
                    } else {
                        String.format(dingReadEntity.text!!, "你", dingReadEntity.receiverName)
                    }
                } else if (TextUtils.equals(msgType, Constants.DING)) {
                    val msgEntity = JsonUtils.toBean(lastMsg, DingMsgEntity::class.java)
                    content = "${ImConfig.MSG_TYPE[Constants.DING]}${msgEntity.content}"
                } else if (TextUtils.equals(msgType, Constants.APPROVE_REASON)) {
                    val valueEntity = JsonUtils.toBean(lastMsg, ApprovalExplainEntity::class.java)
                    content = valueEntity.titleKey + valueEntity.titleValue
                } else if (TextUtils.equals(msgType, Constants.MEETING_RESERVE)) {
                    val reserveEntity = JsonUtils.toBean(lastMsg, ReserveMeetingEntity::class.java)
                    if (null != reserveEntity) {
                        content = reserveEntity.subTitle
                    }
                }
            }
            var txt = lastSetTxtMsg(
                    msgType,
                    lastMsgShowName,
                    groupType,
                    lastMsg,
                    content,
                    lastMsgUserId,
                    lastMsgSenderUserId,
                    lastMsgUserName,
                    isCancel,
                    isAdmin
            )
            if (touchType == 1 || touchType == 2) {
                textView!!.text = lastSetTouchMsg(
                        lastMsg,
                        txt,
                        lastMsgUserName,
                        lastMsgSenderUserId,
                        lastMsgType,
                        lastMsgUserId,
                        touchType,
                        isCancel,
                        msgType
                )
                //是否可以点击
                //textView.setMovementMethod(LinkMovementMethod.getInstance());
                textView!!.highlightColor = Color.parseColor("#00000000") //去掉点击后的背景颜色为透明
                return
            }
            if (touchType == 3 && TextUtils.equals(lastMsgType, SINGLE)) {
                txt = "<font color='#FF6600'>[未接通话]</font>$txt"
                textView!!.text = MyHtml.fromHtml(txt, MyHtml.FROM_HTML_MODE_LEGACY)
                return
            }
            val currentUserId =
                    MkvUtils.get().getString(MkvUtils.UserData, CacheMarker.ImAccount, "")
            val isMe = TextUtils.equals(lastMsgSenderUserId, imAccount)

            //接收者和发送者是否是同一人
            val recipientNotMe = if (TextUtils.isEmpty(recipient)) {
                true
            } else {
                TextUtils.equals(lastMsgSenderUserId, recipient)
            }
            if (isMe && !recipientNotMe && TextUtils.equals(
                            lastMsgType,
                            SINGLE
                    ) && TextUtils.equals(
                            lastMsgSenderUserId,
                            currentUserId
                    )
            ) {
                if (!TextUtils.equals(msgType, NOT_FRIEND)
                        && !TextUtils.equals(msgType, Constants.SYSTEM)
                        && !TextUtils.equals(msgType, Constants.HIDDEN)
                        && !TextUtils.equals(msgType, Constants.DING_READ)
                ) {
                    if (sendStatus == 0) {
                        if (!isCancel) {
                            txt = if (otherIsRead)
                                "<font color='#999999'>[已读]</font>$txt"
                            else
                                "<font color='#0286df'>[未读]</font>$txt"
                        }
                    }
                }
                textView!!.text = MyHtml.fromHtml(txt, MyHtml.FROM_HTML_MODE_LEGACY)
                return
            }
            textView!!.text = MyHtml.fromHtml(txt, MyHtml.FROM_HTML_MODE_LEGACY)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun setGroupJoinStatus(
            textView: TextView?,
            groupSystemTxt: String?,
            groupSystemType: String?,
            groupSystemAccount: String?,
    ) {
        if (TextUtils.isEmpty(groupSystemTxt)) {
            return
        }
        try {
            val entity = JsonUtils.toBean(
                    groupSystemTxt,
                    GroupSystemEntity::class.java
            )
            var name1: String? = ""
            var name2: String? = ""
            val list = findNickNameString(entity, groupSystemAccount)
            var content = ""
            when (groupSystemType) {
                Constants.PASSIVE_EXIT -> {
                    list?.toList()!!.forEach {
                        if (TextUtils.equals(it.userId, entity.handler)) {
                            name1 = Utils.getFromAtMsgSpan(it.userId, it.nickName)
                        } else {
                            name2 = if (TextUtils.isEmpty(name2)) {
                                it.nickName
                            } else {
                                name2 + "、" + it.nickName
                            }
                        }
                    }
                    content = String.format(entity.content!!, name1, name2)
                }
                Constants.INVITE_JOIN, Constants.SCANQRCODE_JOIN -> {
                    list?.toList()!!.forEach {
                        if (TextUtils.equals(it.userId, entity.handler)) {
                            name1 = if (TextUtils.equals(groupSystemAccount, it.userId)) {
                                "你"
                            } else {
                                Utils.getFromAtMsgSpan(it.userId, it.nickName)
                            }
                        } else {
                            name2 = if (TextUtils.isEmpty(name2)) {
                                if (TextUtils.equals(groupSystemAccount, it.userId)) {
                                    "你"
                                } else {
                                    Utils.getFromAtMsgSpan(it.userId, it.nickName)
                                }
                            } else {
                                if (TextUtils.equals(groupSystemAccount, it.userId)) {
                                    "$name2、你"
                                } else {
                                    name2 + "、" + Utils.getFromAtMsgSpan(it.userId, it.nickName)
                                }
                            }
                        }
                    }
                    content = String.format(entity.content!!, name1, name2)
                }
                Constants.ACTIVE_EXIT, Constants.NORMAL_JOIN, Constants.TRANSFER_OWN, Constants.INITIATE_GROUP -> {
                    list?.toList()!!.forEach {
                        if (TextUtils.equals(entity.handler, it.userId)) {
                            name1 = if (TextUtils.equals(groupSystemAccount, it.userId)) {
                                "你"
                            } else {
                                Utils.getFromAtMsgSpan(it.userId, it.nickName)
                            }
                        }
                    }
                    content = String.format(entity.content!!, name1)
                }
            }
            val spanned = MyHtml.fromHtml(content, MyHtml.FROM_HTML_MODE_LEGACY)
            val spannableString = SpannableStringBuilder(spanned)
            EmojiUtil.identifyFaceExpressionAt(
                    AppHelperUtils.instance.getApp(),
                    textView,
                    spannableString,
                    ImageSpan.ALIGN_BOTTOM
            )
            textView!!.movementMethod =
                    LinkMovementMethod.getInstance() //开启此监听注意会与textview autoLink=web属性冲突，

            ClickSpanUtils.clickSpanListener = object : ClickSpanListener {
                override fun clickRsulte(text: String?) {
                    val bundle1 = Bundle()
                    bundle1.putString("fromImAccount", text)
                    bundle1.putString("sourceType", FriendRequestsConfig.SOURCE_TYPE_3)
                    RouteHelper.navigateTo(ContactsRouterPath.PersonCardCompany, bundle1)
                }
            }
            textView!!.highlightColor = Color.parseColor("#00000000") //去掉点击后的背景颜色为透明
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }

    private fun lastSetTxtMsg(
            msgType: String?,
            lastMsgShowName: Boolean,
            groupType: String?,
            lastMsg: String?,
            content: String?,
            lastMsgUserId: String?,
            lastMsgSenderUserId: String?,
            lastMsgUserName: String?,
            isCancel: Boolean,
            isAdmin: Boolean,
    ): String? {
        var lastMsgShowName = lastMsgShowName
        var content: String? = content
        if (msgType == Constants.SYSTEM) {
            lastMsgShowName = false
        }
        //工作通知类型的会话，从lastMessage中取content并将其转为String[]，取index=0
        if (TextUtils.equals(WORK_NOTIFY, groupType)) {
            try {
                val jsonObject = JSONObject(lastMsg)
                val contentStr = jsonObject.getString("content")
                val split = contentStr.split(",").toTypedArray()
                content = split[0]
            } catch (e: JSONException) {
                e.printStackTrace()
            }
        }
        //新成员申请显示问题
        if (!TextUtils.isEmpty(lastMsg) && lastMsg!!.contains(SYS_ROBOT_MEMBER_APPLY)) {
            try {
                val jsonObject = JSONObject(lastMsg)
                content = jsonObject.getString("text")
                if (content.contains(SYS_ROBOT_MEMBER_APPLY)) {
                    val fromJson = JsonUtils.toBean(content, NewMemberContent::class.java)
                    if (!TextUtils.isEmpty(fromJson.flag)) {
                        content = fromJson.text
                    }
                }
            } catch (e: java.lang.Exception) {
                e.printStackTrace()
            }
        }
        //撤回 我们特殊处理
        //此消息是不是自己发送的
        var txt: String? = ""
        val equals = TextUtils.equals(lastMsgUserId, lastMsgSenderUserId)
        txt = if (equals && isCancel) { //撤回自己
            if (isAdmin) {
                "群主或群管理员撤回了一条消息"
            } else {
                "你$content"
            }
        } else if (!equals && isCancel) { //撤回 自己看
            if (isAdmin) {
                "群主或群管理员撤回了一条消息"
            } else {
                lastMsgUserName + content
            }
        } else if (lastMsgShowName) {
            if (filterSystemMessages[msgType] == null) {
                "$lastMsgUserName：$content"
            } else {
                content
            }
        } else {
            content
        }
        return txt
    }

    private fun lastSetTouchMsg(
            lastMsg: String?,
            lastTxt: String?,
            lastMsgUserName: String?,
            lastMsgSenderUserId: String?,
            lastMsgType: String?,
            lastMsgUserId: String?,
            touchType: Int,
            isCancel: Boolean,
            msgType: String?,
    ): SpannableStringBuilder? {
        var string = ""
        if (TextUtils.equals(msgType, Constants.TOUCH)) {
            if (lastMsg?.startsWith("{") == true) {
                val chatEntity = JsonUtils.toBean(lastMsg, TouchChatEntity::class.java)
                if (chatEntity != null) {
                    chatEntity.content?.let { string = it }
                }
            }
        }
        val text = StringBuilder()
        val tag = StringBuilder()
        var startNum = 0
        if (TextUtils.equals(GROUP, lastMsgType)) {
            if (touchType == 2) {
                tag.append("<font color='#FF6600'>[@所有人]</font>")
                startNum = 6
            } else if (touchType == 1) {
                tag.append("<font color='#FF6600'>[有人@我]</font>")
                startNum = 6
            }
            if (!TextUtils.equals(lastMsgSenderUserId, lastMsgUserId) && !isCancel) {
                tag.append(lastMsgUserName).append("：")
            }
        }
        if (TextUtils.isEmpty(string)) {
            text.append(tag)
            val index = lastTxt?.indexOf("：")
            if (index != -1) {
                if (lastTxt != null && index != null) {
                    text.append(lastTxt.substring((index + 1)))
                }
            } else {
                text.append(lastTxt)
            }
        } else {
            text.append(tag)
            text.append(string)
        }
        val spanned = MyHtml.fromHtml(text.toString(), MyHtml.FROM_HTML_MODE_LEGACY)
        val ssb = SpannableStringBuilder(spanned)
        ssb.setSpan(
                ForegroundColorSpan(Color.parseColor("#C7C7C7")),
                startNum,
                spanned.length,
                Spanned.SPAN_INCLUSIVE_INCLUSIVE
        )
        return ssb
    }

    private fun findNickNameString(
            entity: GroupSystemEntity,
            groupSystemAccount: String?,
    ): List<GroupSystemUsersEntity>? {
        val list: MutableList<GroupSystemUsersEntity> = ArrayList()
        for (groupSystemListEntity in entity.systemInfoList!!) {
            if (TextUtils.equals(groupSystemListEntity.userId, groupSystemAccount)) {
                list.addAll(groupSystemListEntity.users!!)
                return list
            }
        }
        return list
    }
}